<template>
	<view class="exchange-page">
		<z-paging ref="paging" v-model="dataList" @query="queryList" :height="scrollViewHeight+'px'">
			<view slot="top">
				<view class="tab-list">
					<view :class="tabsIndex==index?'tab-item active':'tab-item'" v-for="(item,index) in tabsList"
						:key="index" @click="tabsChange(index)">
						<view class="title">{{item.name}}</view>
					</view>
				</view>
			</view>
			<view class="exchange-list">
				<view class="exchange-item" v-for="(item,index) in dataList" :key="index"
					@click="$Tips('/pages/mine/exchangeDetail?order_id='+item.id+'&page='+(tabsIndex==0?'course':'goods'))">
					<image :src="item.image" class="item-left"></image>
					<view class="item-center">
						<view class="item-title">{{item.name}}</view>
						<view class="item-time" v-if="item.pay_type==1">购买时间：{{item.createtime}}</view>
						<view class="item-time" v-if="item.pay_type==2">兑换时间：{{item.createtime}}</view>
					</view>
					<view class="item-right">
						<view class="score" v-if="item.pay_type==1">￥{{item.pay_money}}</view>
						<view class="score" v-if="item.pay_type==2">{{item.pay_money}}积分</view>
						<view class="button button1" v-if="item.status==1&&item.pay_type==1"
							@click.stop="orderRefund(item)">
							申请退款</view>
						<view class="button button1" v-if="item.status==1&&item.pay_type==2"
							@click.stop="orderRefund(item)">
							取消兑换</view>
						<view class="button button2" v-if="item.status==2">进行中</view>
						<view class="button button2" v-if="item.status==3">申请中</view>
						<view class="button button3" v-if="item.status==4">已退款</view>
						<view class="button button3" v-if="item.status==5">已完成</view>
					</view>
				</view>
			</view>
		</z-paging>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabsList: [{
					name: '课程兑换',
					status: '1',
				}, {
					name: '商品兑换',
					status: '2',
				}],
				tabsIndex: 0,
				dataList: [],

			}
		},
		onLoad(options) {

		},
		onShow() {
			this.refreshList()
		},
		methods: {
			orderRefund(item) {
				let that = this
				uni.showModal({
					title: "提示",
					content: "是否取消该商品的兑换/购买",
					success(res) {
						if (res.confirm) {
							if (that.tabsIndex == 0) {
								that.$Api.user.courseOrderRefund({
									order_id: item.id,
								}).then(res => {
									that.$Toast('审核中，请稍后')
									that.refreshList()
								})
							} else {
								that.$Api.user.goodsOrderRefund({
									order_id: item.id,
								}).then(res => {
									that.$Toast('审核中，请稍后')
									that.refreshList()
								})
							}
						}
					}
				})
			},
			tabsChange(index) {
				this.tabsIndex = index
				this.refreshList()
			},
			refreshList() {
				this.$refs.paging.scrollToTop()
				this.$refs.paging.refresh();
			},
			queryList(pageNo, pageSize) {
				if (this.tabsIndex == 0) {
					this.$Api.user.courseOrderList({
						page: pageNo,
					}).then(res => {
						this.$refs.paging.complete(res.data.data);
					}).catch(res => {
						this.$refs.paging.complete(false);
					})
				} else {
					this.$Api.user.goodsOrderList({
						page: pageNo,
					}).then(res => {
						this.$refs.paging.complete(res.data.data);
					}).catch(res => {
						this.$refs.paging.complete(false);
					})
				}

			}
		}
	}
</script>

<style lang="less" scoped>
	.exchange-page {
		width: 100%;
		min-height: 100vh;
		background: #F6F6F8;

		.tab-list {
			min-height: 86rpx;
			margin: 20rpx 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.tab-item {
				width: 332rpx;
				height: 88rpx;
				text-align: center;
				background: linear-gradient(180deg, #F5FFCF 0%, #FFFFFF 100%);
				box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(0, 0, 0, 0.1);
				border-radius: 16rpx;

				.title {
					font-size: 32rpx;
					font-weight: bold;
					margin-top: 22rpx;
				}
			}

			.active {
				background: linear-gradient(180deg, #ddf79a 0%, #DDF79A 13%, #A3C616 100%);
				color: #5A7004;
			}
		}

		.exchange-list {
			background-color: #F7F8FA;

			.exchange-item {
				margin: 0 30rpx 20rpx;
				border-radius: 28rpx;
				padding: 20rpx;
				display: flex;
				align-items: center;
				background: #ffffff;
				box-shadow: 0rpx 0rpx 12rpx 0rpx rgba(0, 0, 0, 0.1), inset 0rpx 2rpx 2rpx 0rpx #FFFFFF;

				.item-left {
					width: 156rpx;
					height: 156rpx;
					margin-right: 26rpx;
				}

				.item-center {
					flex: 1;
					height: 156rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;

					.item-title {
						font-weight: bold;
						font-size: 32rpx;
						width: 300rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.exchange-already {
						font-size: 26rpx;
						color: #565656;
						margin-top: 6rpx;
					}

					.item-time {
						font-size: 26rpx;
						color: #565656;
						margin-top: 16rpx;
					}
				}

				.item-right {
					min-width: 200rpx;
					min-height: 156rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					align-items: flex-end;

					.score {
						font-size: 36rpx;
						color: #FD463E;
						text-align: right;
					}

					.button {
						width: 156rpx;
						height: 56rpx;
						line-height: 56rpx;
						text-align: center;
						border-radius: 44rpx;
					}

					.button1 {
						color: #FFB900;
						background: #FFF8E5;
						border: 1rpx solid #FFB900;
					}

					.button2 {
						color: #A3C616;
						background: #FBFFED;
						border: 1rpx solid #A3C616;
					}

					.button3 {
						color: #DADADA;
						background: #F9F9F9;
						border: 1rpx solid #E5E5E5;
					}
				}
			}
		}
	}
</style>